<!-- 友链编辑弹窗 -->
<template>
  <el-dialog :title="isUpdate ? '修改云挂机CDK' : '添加云挂机CDK激活码'" :visible="visible" width="760px" :destroy-on-close="true"
    :lock-scroll="false" :close-on-click-modal="false" :before-close="confirmClose" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="98px">
      <el-form-item label="批次名称:" prop="batch_id">
        <!-- <PageSelect :placeholder="'请选择激活码批次'" v-model="form.batch_id" :whereName="'name'" :url="'/BoxYunCdkBatch/index'"
          :optionLable="'name'" :optionvalue="'id'" :multiple="false" /> -->
        <el-select clearable v-model="form.batch_id" placeholder="请选择激活码批次" class="ele-fluid" @change="BatchChange">
          <el-option v-for="(item, index) in batchList" :key="index" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="CDK前缀:" prop="prefix">
        <el-input clearable :maxlength="20" v-model="form.prefix" placeholder="请输入批次前缀" :disabled="isInsert" />
      </el-form-item>
      <el-form-item label="CDK数量:" prop="cdk_num">
        <el-input clearable :maxlength="20" type="number" v-model="form.cdk_num" placeholder="请输入生成的CDK数量" />
      </el-form-item>
      <el-form-item label="CDK有效期:" prop="time_range">
        <el-date-picker style="width: 100%" v-model="form.time_range" type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss" label-format="yyyy-MM-dd HH:mm:ss" range-separator="至"
          start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="CDK时长:" prop="duration">
        <el-input clearable :maxlength="20" type="number" v-model="form.duration" placeholder="请输入CDK时长"
          :disabled="isInsert" />
      </el-form-item>
      <el-form-item label="使用限制:" prop="use_limit">
        <el-input clearable :maxlength="20" type="number" v-model="form.use_limit" placeholder="请输入使用上限"
          :disabled="isInsert" />
      </el-form-item>
      <el-form-item label="使用方式:" prop="use_type">
        <el-select v-model="form.use_type" placeholder="请选择" width="100%" :disabled="isInsert">
          <el-option label="不限制" :value=0 />
          <el-option label="仅新增" :value=1 />
          <el-option label="仅续费" :value=2 />
        </el-select>
      </el-form-item>
      <el-form-item label="备注内容:" prop="desc" style="margin-top: 20px">
        <el-input :rows="3" clearable type="textarea" :maxlength="200" v-model="form.desc" placeholder="请输入备注内容" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
// import axios from 'axios';
export default {
  name: "YunGoodsCdkEdit",
  // components: { axios },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    editData: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.editData),
      // 表单验证规则
      rules: {
        batch_id: [
          {
            required: true,
            message: "请选择激活码批次名",
            trigger: "blur"
          }
        ],
        prefix: [
          {
            required: true,
            message: "请输入批次前缀",
            trigger: "blur"
          }
        ],
        cdk_num: [
          {
            required: true,
            message: "请输入cdk数量",
            trigger: "blur"
          }
        ],
        time_range: [
          {
            required: true,
            message: "请选择有效时间",
            trigger: "blur"
          }
        ],
        duration: [
          {
            required: true,
            message: "请输入使用时长 (单位: 天)",
            trigger: "blur"
          }
        ],
        use_limit: [
          {
            required: true,
            message: "请输入兑换限制数量",
            trigger: "blur"
          }
        ],
        use_type: [
          {
            required: true,
            message: "请选择使用方式",
            trigger: "blur"
          }
        ],

        status: [
          { required: true, message: "请选择上架状态", trigger: "blur" }
        ],

      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      types: [
        {
          value: 0,
          label: "普通商品"
        },
        {
          value: 1,
          label: "活动商品"
        }
      ],
      batchList: [], // 从API获取的原始数据列表
      isInsert: false,
    };
  },
  watch: {
    visible(val) {
      if(val){
        this.getYunBatch()
      }
    },
  },

 

  methods: {
    /* 保存编辑 */
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/BoxYunCdkCode/edit", this.form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },

    confirmClose() {
      this.$confirm("确定要返回吗？", "温馨提示")
        .then(() => {
          this.updateVisible(false);
          if (!this.isUpdate) {
            this.form = {};
          }
        })
        .catch(() => { });
    },

    BatchChange() {
      if (this.form.batch_id == '9999999') {
        this.form = {}
        this.form.batch_id = '9999999'
        this.isInsert = false
      } else {
        console.log('index', this.form.batch_id);
        this.batchList.forEach((item) => {
          if (this.form.batch_id == item.id) {
            this.form.prefix = item.prefix
            this.form.duration = item.duration
            this.form.use_limit = item.use_limit
            this.form.use_type = item.use_type
            this.isInsert = true
          }

        })
      }
    },

    async getYunBatch() {
    const response = await this.axios.get('/BoxYunCdkBatch/index', {
      params: {
        page: 1,
        limit: 9999999, // 考虑实际情况调整这个值
      }
    }
    );
    this.batchList = [
      { id: '9999999', name: '快速创建' },
      ...response.data.data,
    ];
    console.log(this.batchList);
  },
  }
};
</script>

<style scoped></style>
